﻿
// PE_StudioDlg.cpp: 实现文件
//

#include "pch.h"
#include "PE_Studio.h"
#include "PE_StudioDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

//DlgRvaRaw		m_RvaRaw;					//RVARAW转换器

// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_ABOUTBOX };
#endif

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
public:
	afx_msg void OnBnClickedOk();
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

void CAboutDlg::OnBnClickedOk()
{
    // TODO: 在此添加控件通知处理程序代码
    CDialogEx::OnOK();
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
	ON_BN_CLICKED(IDOK, &CAboutDlg::OnBnClickedOk)
END_MESSAGE_MAP()



// CPEStudioDlg 对话框

CPEStudioDlg::CPEStudioDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_MAIN, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	
}

void CPEStudioDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_PE_STUDIO_TAB, m_tab);
}

BEGIN_MESSAGE_MAP(CPEStudioDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_NOTIFY(TCN_SELCHANGE, IDC_PE_STUDIO_TAB, &CPEStudioDlg::OnTcnSelchangePetab)
	ON_COMMAND(ID_32771, &CPEStudioDlg::OpenFile)
	ON_COMMAND(ID_32772, &CPEStudioDlg::CloseApp)
	ON_COMMAND(ID_32778, &CPEStudioDlg::OnAbout)
	ON_WM_DROPFILES()
	ON_BN_CLICKED(IDC_BTNTEST, &CPEStudioDlg::OnBnClickedBtntest)
	ON_BN_CLICKED(IDC_BTNRVARAW, &CPEStudioDlg::OnBnClickedBtnrvaraw)
END_MESSAGE_MAP()

// CPEStudioDlg 消息处理程序
BOOL CPEStudioDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != nullptr)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码

    // 加载菜单加速键
	m_hAccel = ::LoadAccelerators(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_MAINFRAME));

    // PE类实例化
    m_pPE = new PE;

    /*添加标签类*/
	//为Tab Control增加两个页面
    m_tab.InsertItem(DLG_NTHEADER, TEXT("NT文件头"));
    m_tab.InsertItem(DLG_DOSHEADER, TEXT("DOS文件头"));
    m_tab.InsertItem(DLG_DATADIR, TEXT("数据目录表"));
	m_tab.InsertItem(DLG_SECTION, TEXT("区段"));
	m_tab.InsertItem(DLG_PROCESS, TEXT("进程"));
    
    //创建3个对话框
    m_peHeader.Create(IDD_DLG_NTHEADER, &m_tab);
    m_PeDos.Create(IDD_DLG_DOSHEADER, &m_tab);
	m_DataDir.Create(IDD_DLG_DATADIR, &m_tab);
	m_Section.Create(IDD_DLG_SECTION, &m_tab);
	m_Process.Create(IDD_DLG_PROCESS, &m_tab);
    
	//将子窗口，设定在Tab内显示的范围
    CRect rc;
    m_tab.GetClientRect(rc);
    rc.top += 20;
    rc.bottom -= 0;
    rc.left += 0;
    rc.right -= 0;
    
	m_peHeader.MoveWindow(&rc);
	m_PeDos.MoveWindow(&rc);
	m_DataDir.MoveWindow(&rc);
	m_Section.MoveWindow(&rc);
	m_Process.MoveWindow(&rc);
    
    //把对话框对象指针保存起来
    pDialog[DLG_NTHEADER] = &m_peHeader;
    pDialog[DLG_DOSHEADER] = &m_PeDos;
    pDialog[DLG_DATADIR] = &m_DataDir;
    pDialog[DLG_SECTION] = &m_Section;    
    pDialog[DLG_PROCESS] = &m_Process;    
    
    //显示初始页面
    pDialog[DLG_DOSHEADER]->ShowWindow(SW_HIDE);
    pDialog[DLG_NTHEADER]->ShowWindow(SW_SHOW);
    pDialog[DLG_DATADIR]->ShowWindow(SW_HIDE);
    pDialog[DLG_SECTION]->ShowWindow(SW_HIDE); 
    pDialog[DLG_PROCESS]->ShowWindow(SW_HIDE);

	// 初始化相关控件	
	if (!InitControl())
	{
		MessageBox(_T("控件文本初始化失败！"));
	}
    
	//列表控件的初始化：设置属性，整行选中，显示风格
	m_Section.ListCtrlSection.SetListCtrl(TRUE, TRUE, FALSE, FALSE);

    //列表控件使用
	CString str[] = { TEXT(""), TEXT("序号"), TEXT("名称"), TEXT("虚拟大小"), TEXT("虚拟偏移"), TEXT("实际大小"), TEXT("实际偏移"), TEXT("特征码") };
	//设置表头
	for (int i = 0; i < sizeof(str) / sizeof(str[0]); i++)
	{
		m_Section.ListCtrlSection.InsertColumn(i, str[i], LVCFMT_CENTER, 80);
	}
	m_Section.ListCtrlSection.DeleteColumn(0);

	//保存当前选择标签
	m_CurSelTab = 1;
    
	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}


void CPEStudioDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CPEStudioDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CPEStudioDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CPEStudioDlg::OnTcnSelchangePetab(NMHDR* pNMHDR, LRESULT* pResult)
{
	// TODO: 在此添加控件通知处理程序代码
    //将当前页面隐藏
	pDialog[m_CurSelTab]->ShowWindow(SW_HIDE);
	//获取目前新的页面选择
    m_CurSelTab = m_tab.GetCurSel();
    //显示新的页面
    pDialog[m_CurSelTab]->ShowWindow(SW_SHOW);    
    
	*pResult = 0;
}

void CPEStudioDlg::CloseApp()
{
	// TODO: 在此添加命令处理程序代码
    delete m_pPE;
    m_pPE = NULL;
	OnOK();
}

void CPEStudioDlg::OnAbout()
{
	// TODO: 在此添加命令处理程序代码
	CAboutDlg AboutDlg;
	AboutDlg.DoModal();
}
/*
typedef struct tagOFNA {
  DWORD         lStructSize;
  HWND          hwndOwner;
  HINSTANCE     hInstance;
  LPCSTR        lpstrFilter;
  LPSTR         lpstrCustomFilter;
  DWORD         nMaxCustFilter;
  DWORD         nFilterIndex;
  LPSTR         lpstrFile;
  DWORD         nMaxFile;
  LPSTR         lpstrFileTitle;
  DWORD         nMaxFileTitle;
  LPCSTR        lpstrInitialDir;
  LPCSTR        lpstrTitle;
  DWORD         Flags;
  WORD          nFileOffset;
  WORD          nFileExtension;
  LPCSTR        lpstrDefExt;
  LPARAM        lCustData;
  LPOFNHOOKPROC lpfnHook;
  LPCSTR        lpTemplateName;
  LPEDITMENU    lpEditInfo;
  LPCSTR        lpstrPrompt;
  void          *pvReserved;
  DWORD         dwReserved;
  DWORD         FlagsEx;
} OPENFILENAMEA, *LPOPENFILENAMEA;
*/
void CPEStudioDlg::OpenFile()
{
	// TODO: 在此添加命令处理程序代码
	
	//TCHAR m_szFilePath[MAX_PATH] = _T("");
    OPENFILENAME ofn = { 0 };
	ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner = m_hWnd;
    ofn.hInstance = GetModuleHandle(NULL);
	ofn.lpstrFilter = TEXT("PE文件(*.exe,*.dll)\0*.exe;*.dll\0所有文件(*.*)\0*.*\0");
    ofn.lpstrFile = m_szFilePath;
	ofn.nMaxFile = MAX_PATH;
	ofn.lpstrInitialDir = _T(".");
    ofn.lpstrTitle = TEXT("打开文件");
    ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
    if (GetOpenFileName(&ofn))
    {
		//MessageBox(TEXT("测试菜单"));
    }
    
    // 开始处理数据：PE文件头、输入输入表等
	if (!InitPE(m_szFilePath))
	{
		MessageBox(_T("PE初始化失败！"));
	}
}

BOOL CPEStudioDlg::PreTranslateMessage(MSG* pMsg)
{
	// TODO: 在此添加专用代码和/或调用基类
	if (TranslateAccelerator(GetSafeHwnd(), m_hAccel, pMsg))
        return TRUE;
	return CDialogEx::PreTranslateMessage(pMsg);
}
// 
void CPEStudioDlg::OnDropFiles(HDROP hDropInfo)
{
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    //TCHAR m_szFilePath[MAX_PATH] = _T("");
    DragQueryFile(hDropInfo, 0, m_szFilePath, MAX_PATH);
    
    // 开始处理数据：PE文件头、输入输入表等
	if (!InitPE(m_szFilePath))
	{
		MessageBox(_T("PE初始化失败！"));
	}
    
    CDialogEx::OnDropFiles(hDropInfo);
}

BOOL CPEStudioDlg::InitPE(TCHAR* m_szFilePath)
{
    m_pPE->m_strFilePath = m_szFilePath;											//获取文件路径
    GetDlgItem(IDC_FILEPATH)->SetWindowText(m_szFilePath);						//设置文本框内容

    m_pPE->UnLoadFile();
    BOOL bRet = m_pPE->LoadFile();
    if (bRet)
    {
        if (m_pPE->InitPeFile())
        {
            ShowDosHeaderInfo();
			ShowFileHeaderInfo();
			ShowOptionHeaderInfo();
			ShowDataDirInfo();
			ShowSectionInfo();
            //NT文件头窗口控件
            GetDlgItem(IDC_BTNTEST)->EnableWindow(TRUE);
            GetDlgItem(IDC_BTNRVARAW)->EnableWindow(TRUE);
            //数据目录表窗口控件
            pDialog[DLG_DATADIR]->GetDlgItem(IDC_BTN_IMPORT)->EnableWindow(TRUE);
            pDialog[DLG_DATADIR]->GetDlgItem(IDC_BTN_EXPORT)->EnableWindow(TRUE);
        }
        else
        {
            MessageBox(_T("非PE文件！"));
			return FALSE;
        }
    }
	return TRUE;
}

BOOL CPEStudioDlg::InitControl()
{
	//禁用控件
    //NT文件头窗口控件
	GetDlgItem(IDC_BTNTEST)->EnableWindow(FALSE);
	GetDlgItem(IDC_BTNRVARAW)->EnableWindow(FALSE);    
    //数据目录表窗口控件
    pDialog[DLG_DATADIR]->GetDlgItem(IDC_BTN_IMPORT)->EnableWindow(FALSE);
    pDialog[DLG_DATADIR]->GetDlgItem(IDC_BTN_EXPORT)->EnableWindow(FALSE);
	
	return TRUE;
}

BOOL CPEStudioDlg::ShowDosHeaderInfo()
{
    PIMAGE_DOS_HEADER pDosHeader = m_pPE->m_pDosHeader;
    if (NULL == pDosHeader)
    {
        MessageBox(_T("DOS头为空，显示失败！"));
        return FALSE;
    }
    else
    {
        CString strTemp = _T("");
        TCHAR szBuffer1[8] = { 0 };
        TCHAR szBuffer2[20] = { 0 };

        // 幻数
        strTemp.Format(_T("%04X"), pDosHeader->e_magic);
        pDialog[DLG_DOSHEADER]->SetDlgItemText(IDC_EDIT_DOS_MAGIC, strTemp);

        // 最后单面字节
        strTemp.Format(_T("%04X"), pDosHeader->e_cblp);
        pDialog[DLG_DOSHEADER]->SetDlgItemText(IDC_EDIT_DOS_CBLP, strTemp);

        // 文件中的页面
        strTemp.Format(_T("%04X"), pDosHeader->e_cp);
        pDialog[DLG_DOSHEADER]->SetDlgItemText(IDC_EDIT_DOS_CP, strTemp);

        // 重定位
        strTemp.Format(_T("%04X"), pDosHeader->e_crlc);
        pDialog[DLG_DOSHEADER]->SetDlgItemText(IDC_EDIT_DOS_CRLC, strTemp);

        // 段落文件头的大小
        strTemp.Format(_T("%04X"), pDosHeader->e_cparhdr);
        pDialog[DLG_DOSHEADER]->SetDlgItemText(IDC_EDIT_DOS_CPARHDR, strTemp);

        // 最小所需额外段落
        strTemp.Format(_T("%04X"), pDosHeader->e_minalloc);
        pDialog[DLG_DOSHEADER]->SetDlgItemText(IDC_EDIT_DOS_MINALLOC, strTemp);

        // 最大所需额外段落
        strTemp.Format(_T("%04X"), pDosHeader->e_maxalloc);
        pDialog[DLG_DOSHEADER]->SetDlgItemText(IDC_EDIT_DOS_MAXALLOC, strTemp);

        // 初始SS值
        strTemp.Format(_T("%04X"), pDosHeader->e_ss);
        pDialog[DLG_DOSHEADER]->SetDlgItemText(IDC_EDIT_DOS_SS, strTemp);

        // 初始SP值 
        strTemp.Format(_T("%04X"), pDosHeader->e_sp);
        pDialog[DLG_DOSHEADER]->SetDlgItemText(IDC_EDIT_DOS_SP, strTemp);

        // 校验和
        strTemp.Format(_T("%04X"), pDosHeader->e_csum);
        pDialog[DLG_DOSHEADER]->SetDlgItemText(IDC_EDIT_DOS_CSUM, strTemp);

        // 初始IP值
        strTemp.Format(_T("%04X"), pDosHeader->e_ip);
        pDialog[DLG_DOSHEADER]->SetDlgItemText(IDC_EDIT_DOS_IP, strTemp);

        // 初始CS值 
        strTemp.Format(_T("%04X"), pDosHeader->e_cs);
        pDialog[DLG_DOSHEADER]->SetDlgItemText(IDC_EDIT_DOS_CS, strTemp);

        // 重定位表地址
        strTemp.Format(_T("%04X"), pDosHeader->e_lfarlc);
        pDialog[DLG_DOSHEADER]->SetDlgItemText(IDC_EDIT_DOS_LFARLC, strTemp);

        // 复盖数
        strTemp.Format(_T("%04X"), pDosHeader->e_ovno);
        pDialog[DLG_DOSHEADER]->SetDlgItemText(IDC_EDIT_DOS_OVNO, strTemp);

        // 保留

        // OEM验证码
        strTemp.Format(_T("%04X"), pDosHeader->e_oemid);
        pDialog[DLG_DOSHEADER]->SetDlgItemText(IDC_EDIT_DOS_OEMID, strTemp);

        // OEM信息
        strTemp.Format(_T("%04X"), pDosHeader->e_oeminfo);
        pDialog[DLG_DOSHEADER]->SetDlgItemText(IDC_EDIT_DOS_OEMINFO, strTemp);

        // 保留

        // PE文件头地址
        strTemp.Format(_T("%08X"), pDosHeader->e_lfanew);
        pDialog[DLG_DOSHEADER]->SetDlgItemText(IDC_EDIT_DOS_LFANEW, strTemp);
    }
}

BOOL CPEStudioDlg::ShowFileHeaderInfo()
{
	PIMAGE_FILE_HEADER pFileHeader = m_pPE->m_pFileHeader;
	if (NULL == pFileHeader)
	{
		MessageBox(_T("文件头为空，显示失败！"));
		return FALSE;
	}
    else
    {
        CString strTemp = _T("");

		// 运行平台
        strTemp.Format(_T("%04X"), pFileHeader->Machine);
        pDialog[DLG_NTHEADER]->SetDlgItemText(IDC_EDIT_FL_MACHINE, strTemp);

		// 区段数目        
        strTemp.Format(_T("%04X"), pFileHeader->NumberOfSections);
        pDialog[DLG_NTHEADER]->SetDlgItemText(IDC_EDIT_FL_SECNUMS, strTemp);
        
        // 创建日期和时间
        strTemp.Format(_T("%08X"), pFileHeader->TimeDateStamp);
        pDialog[DLG_NTHEADER]->SetDlgItemText(IDC_EDIT_FL_TDS, strTemp);

        // 符号表
        strTemp.Format(_T("%08X"), pFileHeader->PointerToSymbolTable);
        pDialog[DLG_NTHEADER]->SetDlgItemText(IDC_EDIT_FL_PTRTOSYMLTBL, strTemp);

        // 符号数量
        strTemp.Format(_T("%08X"), pFileHeader->NumberOfSymbols);
        pDialog[DLG_NTHEADER]->SetDlgItemText(IDC_EDIT_FL_NUMOFSYMBOLS, strTemp);

        // 可选头大小
        strTemp.Format(_T("%08X"), pFileHeader->SizeOfOptionalHeader);
        pDialog[DLG_NTHEADER]->SetDlgItemText(IDC_EDIT_FL_SIZEOFOPTHEADER, strTemp);

		// 特征值 (Characteristics)
        strTemp.Format(_T("%04X"), pFileHeader->Characteristics);
        pDialog[DLG_NTHEADER]->SetDlgItemText(IDC_EDIT_FL_CHAR, strTemp);
        
    }
	return TRUE;
}

BOOL CPEStudioDlg::ShowOptionHeaderInfo()
{
	PIMAGE_OPTIONAL_HEADER pOptionHeader = m_pPE->m_pOptionalHeader;
	if (NULL == pOptionHeader)
	{
		MessageBox(_T("可选头为空，显示失败！"));
		return FALSE;
	}
	else
    {
        CString strTemp = _T("");
        DWORD dwTemp = 0;
        // 幻数
        strTemp.Format(_T("%04X"), pOptionHeader->Magic);
        pDialog[DLG_NTHEADER]->SetDlgItemText(IDC_EDIT_OP_MAGIC, strTemp);

        // 主要链接器
        strTemp.Format(_T("%02X"), pOptionHeader->MajorLinkerVersion);
        pDialog[DLG_NTHEADER]->SetDlgItemText(IDC_EDIT_OP_MAJORLINKER, strTemp);

        // 次要链接器
        strTemp.Format(_T("%02X"), pOptionHeader->MinorLinkerVersion);
        pDialog[DLG_NTHEADER]->SetDlgItemText(IDC_EDIT_OP_MINORLINKER, strTemp);

        // 代码大小
        strTemp.Format(_T("%08X"), pOptionHeader->SizeOfCode);
        pDialog[DLG_NTHEADER]->SetDlgItemText(IDC_EDIT_OP_SIZEOFCODE, strTemp);

        // 初始化数据大小
        strTemp.Format(_T("%08X"), pOptionHeader->SizeOfInitializedData);
        pDialog[DLG_NTHEADER]->SetDlgItemText(IDC_EDIT_OP_INITDATA, strTemp);

        // 未初始化数据大小
        strTemp.Format(_T("%08X"), pOptionHeader->SizeOfUninitializedData);
        pDialog[DLG_NTHEADER]->SetDlgItemText(IDC_EDIT_OP_UNINITDATA, strTemp);

        // 入口点 RVA
		strTemp.Format(_T("%08X"), pOptionHeader->AddressOfEntryPoint);
        pDialog[DLG_NTHEADER]->SetDlgItemText(IDC_EDIT_OP_ENTRYPOIT_RVA, strTemp);
        
		// 入口点 RAW
		dwTemp = m_pPE->RVAtoFOA(pOptionHeader->AddressOfEntryPoint);
		strTemp.Format(_T("%08X"), dwTemp);
        pDialog[DLG_NTHEADER]->SetDlgItemText(IDC_EDIT_OP_ENTRYPOINT_RAW, strTemp);
        
        // 代码起始 RVA
        strTemp.Format(_T("%08X"), pOptionHeader->BaseOfCode);
        pDialog[DLG_NTHEADER]->SetDlgItemText(IDC_EDIT_OP_BASEOFCODE, strTemp);
        
        // 数据起始 RVA
        strTemp.Format(_T("%08X"), pOptionHeader->BaseOfData);
        pDialog[DLG_NTHEADER]->SetDlgItemText(IDC_EDIT_OP_BASEOFDATA, strTemp);

        // 映像基址		(ImageBase)
		strTemp.Format(_T("%08X"), pOptionHeader->ImageBase);
		pDialog[DLG_NTHEADER]->SetDlgItemText(IDC_EDIT_OP_IMAGEBASE, strTemp);
               
		// 区段对齐		(SectionAlignment)
        strTemp.Format(_T("%08X"), pOptionHeader->SectionAlignment);
        pDialog[DLG_NTHEADER]->SetDlgItemText(IDC_EDIT_OP_SECALIGN, strTemp);

		// 文件对齐		(FileAlignment)
        strTemp.Format(_T("%08X"), pOptionHeader->FileAlignment);
        pDialog[DLG_NTHEADER]->SetDlgItemText(IDC_EDIT_OP_FILEALIGN, strTemp);

        // 文件头大小		(SizeOfHeaders)
        strTemp.Format(_T("%08X"), pOptionHeader->SizeOfHeaders);
        pDialog[DLG_NTHEADER]->SetDlgItemText(IDC_EDIT_OP_SIZEOFHEADERS, strTemp);

        // 映像大小		(SizeOfImage)
        strTemp.Format(_T("%08X"), pOptionHeader->SizeOfImage);
        pDialog[DLG_NTHEADER]->SetDlgItemText(IDC_EDIT_OP_SIZEOFIMAGE, strTemp);

        // 校验和		(CheckSum)
        strTemp.Format(_T("%08X"), pOptionHeader->CheckSum);
        pDialog[DLG_NTHEADER]->SetDlgItemText(IDC_EDIT_OP_CSUM, strTemp);

		// Dll特征		(DllCharacteristics)
        strTemp.Format(_T("%04X"), pOptionHeader->DllCharacteristics);
        pDialog[DLG_NTHEADER]->SetDlgItemText(IDC_EDIT_OP_DLLCHAR, strTemp);

        // 堆栈大小
        strTemp.Format(_T("%08X"), pOptionHeader->SizeOfStackReserve);
        pDialog[DLG_NTHEADER]->SetDlgItemText(IDC_EDIT_OP_SIZEOFSTACK, strTemp);

        // 堆栈提交大小
        strTemp.Format(_T("%08X"), pOptionHeader->SizeOfStackCommit);
        pDialog[DLG_NTHEADER]->SetDlgItemText(IDC_EDIT_OP_SIZEOFSTACKCOMMIT, strTemp);

        // 堆保存器大小
        strTemp.Format(_T("%08X"), pOptionHeader->SizeOfHeapReserve);
        pDialog[DLG_NTHEADER]->SetDlgItemText(IDC_EDIT_OP_SIZEOFHEAP, strTemp);

        // 堆提交大小
        strTemp.Format(_T("%08X"), pOptionHeader->SizeOfHeapCommit);
        pDialog[DLG_NTHEADER]->SetDlgItemText(IDC_EDIT_OP_SIZEOFHEAPCOMMIT, strTemp);

        // 数据目录表数量
        strTemp.Format(_T("%08X"), pOptionHeader->NumberOfRvaAndSizes);
        pDialog[DLG_NTHEADER]->SetDlgItemText(IDC_EDIT_OP_NUMOFRVA, strTemp);
	}
    
	return TRUE;
}

BOOL CPEStudioDlg::ShowDataDirInfo()
{
	if (NULL == m_pPE->m_pDataDirectory)
	{
		MessageBox(_T("数据目录表为空，显示失败！"));
		return FALSE;
	}
	else
	{		
		CString strTemp = _T("");
        // 输出表
        strTemp.Format(_T("%08X"), m_pPE->m_vecDataDirectoryTable[0].VirtualAddress);
		pDialog[DLG_DATADIR]->SetDlgItemText(IDC_EDIT_IDE_EXPORT,strTemp);
        strTemp.Format(_T("%08X"), m_pPE->m_vecDataDirectoryTable[0].Size);
        pDialog[DLG_DATADIR]->SetDlgItemText(IDC_EDIT_IDE_EXPORT_SIZE, strTemp);
        
        // 输入表
        strTemp.Format(_T("%08X"), m_pPE->m_vecDataDirectoryTable[1].VirtualAddress);
        pDialog[DLG_DATADIR]->SetDlgItemText(IDC_EDIT_IDE_IMPORT, strTemp);
        strTemp.Format(_T("%08X"), m_pPE->m_vecDataDirectoryTable[1].Size);
        pDialog[DLG_DATADIR]->SetDlgItemText(IDC_EDIT_IDE_IMPORT_SIZE, strTemp);
        
        // 资源表
        strTemp.Format(_T("%08X"), m_pPE->m_vecDataDirectoryTable[2].VirtualAddress);
        pDialog[DLG_DATADIR]->SetDlgItemText(IDC_EDIT_IDE_RESOURCE, strTemp);
        strTemp.Format(_T("%08X"), m_pPE->m_vecDataDirectoryTable[2].Size);
        pDialog[DLG_DATADIR]->SetDlgItemText(IDC_EDIT_IDE_RESOURCE_SIZE, strTemp);
        
        // 异常表
        strTemp.Format(_T("%08X"), m_pPE->m_vecDataDirectoryTable[3].VirtualAddress);
        pDialog[DLG_DATADIR]->SetDlgItemText(IDC_EDIT_IDE_EXCEPTION, strTemp);
        strTemp.Format(_T("%08X"), m_pPE->m_vecDataDirectoryTable[3].Size);
        pDialog[DLG_DATADIR]->SetDlgItemText(IDC_EDIT_IDE_EXCEPTION_SIZE, strTemp);
        
        // 安全表
        strTemp.Format(_T("%08X"), m_pPE->m_vecDataDirectoryTable[4].VirtualAddress);
        pDialog[DLG_DATADIR]->SetDlgItemText(IDC_EDIT_IDE_SECURITY, strTemp);
        strTemp.Format(_T("%08X"), m_pPE->m_vecDataDirectoryTable[4].Size);
        pDialog[DLG_DATADIR]->SetDlgItemText(IDC_EDIT_IDE_SECURITY_SIZE, strTemp);

        // 重定位表
        strTemp.Format(_T("%08X"), m_pPE->m_vecDataDirectoryTable[5].VirtualAddress);
        pDialog[DLG_DATADIR]->SetDlgItemText(IDC_EDIT_IDE_BASERELOC, strTemp);
        strTemp.Format(_T("%08X"), m_pPE->m_vecDataDirectoryTable[5].Size);
        pDialog[DLG_DATADIR]->SetDlgItemText(IDC_EDIT_IDE_BASERELOC_SIZE, strTemp);
        
        // 调试信息
        strTemp.Format(_T("%08X"), m_pPE->m_vecDataDirectoryTable[6].VirtualAddress);
        pDialog[DLG_DATADIR]->SetDlgItemText(IDC_EDIT_IDE_DEBUG, strTemp);
        strTemp.Format(_T("%08X"), m_pPE->m_vecDataDirectoryTable[6].Size);
        pDialog[DLG_DATADIR]->SetDlgItemText(IDC_EDIT_IDE_DEBUG_SIZE, strTemp);

        // 架构信息
        strTemp.Format(_T("%08X"), m_pPE->m_vecDataDirectoryTable[7].VirtualAddress);
        pDialog[DLG_DATADIR]->SetDlgItemText(IDC_EDIT_IDE_ARCH, strTemp);
        strTemp.Format(_T("%08X"), m_pPE->m_vecDataDirectoryTable[7].Size);
        pDialog[DLG_DATADIR]->SetDlgItemText(IDC_EDIT_IDE_ARCH_SIZE, strTemp);
        
        // 全局指针
        strTemp.Format(_T("%08X"), m_pPE->m_vecDataDirectoryTable[8].VirtualAddress);
        pDialog[DLG_DATADIR]->SetDlgItemText(IDC_EDIT_IDE_GLOBALPTR, strTemp);
        strTemp.Format(_T("%08X"), m_pPE->m_vecDataDirectoryTable[8].Size);
        pDialog[DLG_DATADIR]->SetDlgItemText(IDC_EDIT_IDE_GLOBALPTR_SIZE, strTemp);

        // TLS表
        strTemp.Format(_T("%08X"), m_pPE->m_vecDataDirectoryTable[9].VirtualAddress);
        pDialog[DLG_DATADIR]->SetDlgItemText(IDC_EDIT_IDE_TLS, strTemp);
        strTemp.Format(_T("%08X"), m_pPE->m_vecDataDirectoryTable[9].Size);
        pDialog[DLG_DATADIR]->SetDlgItemText(IDC_EDIT_IDE_TLS_SIZE, strTemp);

        // 加载配置
        strTemp.Format(_T("%08X"), m_pPE->m_vecDataDirectoryTable[10].VirtualAddress);
        pDialog[DLG_DATADIR]->SetDlgItemText(IDC_EDIT_IDE_LOAD_CONFIG, strTemp);
        strTemp.Format(_T("%08X"), m_pPE->m_vecDataDirectoryTable[10].Size);
        pDialog[DLG_DATADIR]->SetDlgItemText(IDC_EDIT_IDE_LOAD_CONFIG_SIZE, strTemp);
        
        // 输入范围
        strTemp.Format(_T("%08X"), m_pPE->m_vecDataDirectoryTable[11].VirtualAddress);
        pDialog[DLG_DATADIR]->SetDlgItemText(IDC_EDIT_IDE_BOUND_IMPORT, strTemp);
        strTemp.Format(_T("%08X"), m_pPE->m_vecDataDirectoryTable[11].Size);
        pDialog[DLG_DATADIR]->SetDlgItemText(IDC_EDIT_IDE_BOUND_IMPORT_SIZE, strTemp);
        
        // IAT
        strTemp.Format(_T("%08X"), m_pPE->m_vecDataDirectoryTable[12].VirtualAddress);
        pDialog[DLG_DATADIR]->SetDlgItemText(IDC_EDIT_IDE_IAT, strTemp);
        strTemp.Format(_T("%08X"), m_pPE->m_vecDataDirectoryTable[12].Size);
        pDialog[DLG_DATADIR]->SetDlgItemText(IDC_EDIT_IDE_IAT_SIZE, strTemp);

		// 延迟输入
        strTemp.Format(_T("%08X"), m_pPE->m_vecDataDirectoryTable[13].VirtualAddress);
        pDialog[DLG_DATADIR]->SetDlgItemText(IDC_EDIT_IDE_DELAY_IMPORT, strTemp);
        strTemp.Format(_T("%08X"), m_pPE->m_vecDataDirectoryTable[13].Size);
        pDialog[DLG_DATADIR]->SetDlgItemText(IDC_EDIT_IDE_DELAY_IMPORT_SIZE, strTemp);

        // COM表
        strTemp.Format(_T("%08X"), m_pPE->m_vecDataDirectoryTable[14].VirtualAddress);
        pDialog[DLG_DATADIR]->SetDlgItemText(IDC_EDIT_IDE_COM_DESCRIPTOR, strTemp);
        strTemp.Format(_T("%08X"), m_pPE->m_vecDataDirectoryTable[14].Size);
        pDialog[DLG_DATADIR]->SetDlgItemText(IDC_EDIT_IDE_COM_DESCRIPTOR_SIZE, strTemp);

        // 保留
        strTemp.Format(_T("%08X"), m_pPE->m_vecDataDirectoryTable[15].VirtualAddress);
        pDialog[DLG_DATADIR]->SetDlgItemText(IDC_EDIT_IDE_RESERVE, strTemp);
        strTemp.Format(_T("%08X"), m_pPE->m_vecDataDirectoryTable[15].Size);
        pDialog[DLG_DATADIR]->SetDlgItemText(IDC_EDIT_IDE_RESERVE_SIZE, strTemp);        
	}
    
    return TRUE;
}

BOOL CPEStudioDlg::ShowSectionInfo()
{
	int i;
	CString szTemp = _T("");
	if (m_pPE->m_vecSectionTable.empty())
	{
		MessageBox(_T("区段表为空，显示失败！"));
		return FALSE;
	}
	else
	{
		m_Section.ListCtrlSection.DeleteAllItems();
		for (i = 0; i < m_pPE->m_vecSectionTable.size(); i++)
		{
			szTemp.Format(_T("%d"), i + 1);
			m_Section.ListCtrlSection.InsertItem(i, szTemp);

			m_Section.ListCtrlSection.SetItemText(i, 1, (CString)(m_pPE->m_vecSectionTable[i].Name));

			szTemp.Format(_T("%08X"), m_pPE->m_vecSectionTable[i].Misc.VirtualSize);
            m_Section.ListCtrlSection.SetItemText(i, 2, szTemp);
            
			szTemp.Format(_T("%08X"), m_pPE->m_vecSectionTable[i].VirtualAddress);
            m_Section.ListCtrlSection.SetItemText(i, 3, szTemp);
            
            szTemp.Format(_T("%08X"), m_pPE->m_vecSectionTable[i].SizeOfRawData);
            m_Section.ListCtrlSection.SetItemText(i, 4, szTemp);
            
			szTemp.Format(_T("%08X"), m_pPE->RVAtoFOA(m_pPE->m_vecSectionTable[i].VirtualAddress));
            m_Section.ListCtrlSection.SetItemText(i, 5, szTemp);
            
			szTemp.Format(_T("%08X"), m_pPE->m_vecSectionTable[i].Characteristics);
            m_Section.ListCtrlSection.SetItemText(i, 6, szTemp);
		}
	}
    
    return TRUE;
}

void CPEStudioDlg::OnBnClickedBtntest()
{
	// TODO: 在此添加控件通知处理程序代码
	STARTUPINFO si;
    PROCESS_INFORMATION pi;
    memset(&si, 0, sizeof(si));
	memset(&pi, 0, sizeof(pi));
	si.cb = sizeof(si);
	si.dwFlags = STARTF_USESHOWWINDOW;
    si.wShowWindow = SW_SHOW;    

    CString strCmdLine = _T("");
    strCmdLine.Format(_T("%s"), m_szFilePath);
    if (::CreateProcess(NULL, strCmdLine.GetBuffer(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
    {
        ::CloseHandle(pi.hThread);
        ::CloseHandle(pi.hProcess);
    }
	else
	{
		MessageBox(_T("程序启动失败！"));
	}
}

void CPEStudioDlg::OnBnClickedBtnrvaraw()
{
	// TODO: 在此添加控件通知处理程序代码
	m_RvaRaw.DoModal();
}

